﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.ViewModel;

namespace Mimoza.Apps.Console.Commands
{
    public class RenameContainerCommand : BaseConsoleCommand
    {
        ContainerTreeViewModel m_viewModel = null;
        public RenameContainerCommand(ContainerTreeViewModel viewModel) 
        {
            m_viewModel = viewModel;
            m_viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.view_PropertyChanged);
        }

        void view_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            bool state = false;
            if (m_viewModel.Context != null)
            {
                Container cont = (sender as BaseViewModel).Context as Container;

                if ( !cont.IsSystem )
                {
                    state = true;
                }
            }

            this.Visible = true;
            this.Enabled = state;
        }

        public override void Execute()
        {
            NewContainerCommandForm form = new NewContainerCommandForm();
            Container cont = m_viewModel.Context as Container;
            if (cont != null)
            {
                form.folderName.Text = cont.Name;
                if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    cont.Name = form.folderName.Text;

                    m_viewModel.Application.ObjectLayer.ContainerRepository.Update(cont);
                }
            }
        }
    }
}
